package com.wym.rbac.service.impl;
 // 确保导入接口
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wym.rbac.domain.Department;
import com.wym.rbac.domain.query.QueryObject;
import com.wym.rbac.domain.vo.PageResult;
import com.wym.rbac.mapper.DepartmentMapper;
import com.wym.rbac.service.IDepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> selectAll(){return departmentMapper.selectAll();}

    @Override
    public PageResult<Department>selectByPage(QueryObject qo){
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        PageInfo pageInfo=new PageInfo<>(departmentMapper.queryForList());
        PageResult pageResult=new PageResult();
        BeanUtils.copyProperties(pageInfo,pageResult);
        return pageResult;
    }
    @Override
    public void deleteById(Long id){
        Assert.state(departmentMapper.deleteById(id)>0,"删除失败");
    }
   @Override
    public void saveOrUpdate(Department department){
        if(department.getId()==null){
            Assert.state(departmentMapper.insert(department)>0,
                    "添加失败");
            return;
        }
        Assert.state(departmentMapper.updateById(department)>0,
                "更新失败");
   }
   @Override
    public Department selectById(Long id){
        Assert.notNull(id,"非法参数");
        return departmentMapper.selectById(id);
   }

}
